<?php
/* SVN FILE: $Id: assigned_assessment.php 29 2009-10-04 03:55:23Z kenreily@gmail.com $ */
/**
 * Short description for file.
 *
 * Long description for file
 *
 * PHP versions 4 and 5
 *
 * Recenseo: Online submission and reviewing for education
 * Copyright 2008-2009, Ken Reily
 *
 * Licensed under The MIT License
 * Redistributions of files must retain the above copyright notice.
 *
 * $Date: 2009-10-04 03:55:23 +0000 (Sun, 04 Oct 2009) $
 * $Revision: 29 $
 * $Author: kenreily@gmail.com $
 * $HeadURL: http://recenseo.googlecode.com/svn/trunk/app/models/assigned_assessment.php $
 */
class AssignedAssessment extends AppModel {
  var $name = 'AssignedAssessment';

  var $belongsTo = array('Reviewer' => array('className' => 'User',
					     'foreignKey' => 'reviewer_user_id'),
			 'Reviewee' => array('className' => 'User',
					     'foreignKey' => 'reviewee_user_id'),
			 'Assignment');

  function getForCourse($course_id,$user_id)
  {
    return $this->query("SELECT AssignedAssessment.id,Submission.id,Assignment.name " .
			  "FROM assigned_assessments AS AssignedAssessment " .
			  "JOIN submissions AS Submission ON Submission.user_id = AssignedAssessment.reviewee_user_id AND Submission.assignment_id = AssignedAssessment.assignment_id " .
			  "JOIN assignments AS Assignment ON Assignment.id = Submission.assignment_id " .
			  "JOIN users AS User ON User.id = AssignedAssessment.reviewer_user_id " .
			 "WHERE Submission.active = 1 " .
			   "AND AssignedAssessment.reviewer_user_id = $user_id " .
			   "AND now() > Assignment.due " .
                           "AND (EXISTS (SELECT id FROM submissions AS s WHERE s.assignment_id = Assignment.id AND s.user_id = {$user_id} AND s.active = 1) " . 
			        "OR User.role = 'Instructor' OR User.role = 'TA') " .
			   "AND Assignment.course_id = $course_id " . 
			"ORDER BY Submission.created");
  }

  function getForSubmission($submission_id,$user_id)
  {
    return $this->query("SELECT AssignedAssessment.id,Assignment.id,Assignment.course_id " .
			  "FROM submissions AS Submission " .
			  "JOIN assignments AS Assignment ON Assignment.id = Submission.assignment_id " .
			  "JOIN assigned_assessments AS AssignedAssessment ON AssignedAssessment.assignment_id = Assignment.id " .
			 "WHERE AssignedAssessment.reviewee_user_id = Submission.user_id " .
			   "AND AssignedAssessment.reviewer_user_id = {$user_id} " .
    			   "AND Submission.id = {$submission_id}");
  }

}
?>